package com.bw.day523;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.json.JSONArray;

import java.util.ArrayList;

public class SplitA extends GenericUDTF {


    public StructObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
        ArrayList<String> fieldNames = new ArrayList<String>();
        ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
        fieldNames.add("col1");
        fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,
                fieldOIs);
    }

    @Override
    public void process(Object[] objects) throws HiveException {

        String s = objects[0].toString();   //

        JSONArray jsonArray = new JSONArray(s);
        for (int i = 0; i < jsonArray.length(); i++) {

            Object o = jsonArray.get(i);

            forward(new String[]{o.toString()});

        }

    }

    @Override
    public void close() throws HiveException {

    }

    public static void main(String[] args) {
        JSONArray jsonArray = new JSONArray("[{\"action_id\":\"cart_add\",\"item\":\"33\",\"item_type\":\"sku_id\",\"ts\":1740444169310},{\"action_id\":\"get_coupon\",\"item\":\"3\",\"item_type\":\"coupon_id\",\"ts\":1740444172620}]");
        for (int i = 0; i < jsonArray.length(); i++) {

            Object o = jsonArray.get(i);

            System.out.println(o);

        }
    }
}
